//添加车辆

interface ICarManagentAddViewModel extends ng.IScope {
    /**
     * 承运类别下拉
     */
    carrierCategoryDropDown: ValueListItemResponse[];
    /**
     * 车型下拉
     */
    carTypeDropDown: ValueListItemResponse[];
    /**
     * 车长下拉
     */
    carLengthDropDown: ValueListItemResponse[];
    /**
     * 车牌号
     */
    carCode: string;
    /**
     * 车型
     */
    carType: string;
    /**
     * 车长
     */
    carLength: string;
    /**
     * 驾驶员
     */
    driver: string;
    /**
     * 驾驶员号码
     */
    driverPhoneNumber: string;
    /**
     * 承运类别
     */
    carrierCategory: string;
    /**
     * 核定载重
     */
    bearing: number;
    /**
     * 车辆来源
     */
    carSource: string;
    /**
    * 道路运输许可证号
    */
    permitNumber: string;
    /**
    * 所属辖区
    */
    countrySubdivisionCode: string;
    /**
    * 道路运输证字号
    */
    roadTransportCertificateNumber: string;
    /**
     * 省编号
     */
    provinceCode: string;
    /**
     * 市编号
     */
    cityCode: string;
    /**
     * 区编号
     */
    areaCode: string;
    /**
     * 地址
     */
    address: string;
    /**
     * 身份证号码
     */
    identity: string;
    /**
     * 保存
     */
    click: () => void;
    /**
     * 保存按钮
     */
    saveDis: boolean;

}

class CarManagentAddController {
    gridApi: uiGrid.IGridApiOf<CarListItemResponse>;

    constructor(private $scope: ICarManagentAddViewModel, private carService: ICarService, private valueService: IValueService,
        private $state: angular.ui.IStateService, private $ngBootbox: BootboxService) {
        this.init();
    }

    init(): void {
        this.$scope.saveDis = false;
        this.clearInput();
        this.loadCarLengthDropDown();
        this.loadCarrierCategoryDropDown();
        this.loadCarTypeDropDown();
        
        this.$scope.click = this.addCar;
    }

    /**
     * 加载车长下拉
     */
    loadCarLengthDropDown = () => {
        this.$scope.carLengthDropDown = this.valueService.getCarLengthList().data;
    }

    /**
     * 加载车型下拉
     */
    loadCarTypeDropDown = () => {
        this.$scope.carTypeDropDown = this.valueService.getCarTypeList().data;
    }

    /**
     * 加载承运类别下拉
     */
    loadCarrierCategoryDropDown = () => {
        this.$scope.carrierCategoryDropDown = this.valueService.getCarriageTypeList().data;
    }

    clearInput = () => {
        this.$scope.carCode = "";
        this.$scope.carType = "";
        this.$scope.carLength = "";
        this.$scope.driver = "";
        this.$scope.driverPhoneNumber = "";
        this.$scope.carrierCategory = "";
        this.$scope.bearing = null;
        this.$scope.carSource = "";
        this.$scope.permitNumber = "";
        this.$scope.countrySubdivisionCode = "";
        this.$scope.roadTransportCertificateNumber = "";
        this.$scope.address = "";
        this.$scope.identity = "";
        this.$scope.click = null;
    }

    addCar = () => {
        // if (!$("#form").valid()) { return; }
         if (this.$scope.areaCode !="-1") {
                this.$scope.countrySubdivisionCode = this.$scope.areaCode;
            } else if (this.$scope.areaCode =="-1" && this.$scope.cityCode !="-1") {
                this.$scope.countrySubdivisionCode = this.$scope.cityCode;
            } else if (this.$scope.cityCode =="-1" && this.$scope.provinceCode !="-1") {
                this.$scope.countrySubdivisionCode = this.$scope.provinceCode;
            }
        this.$ngBootbox.confirm("确定保存数据吗？").then(result => {
            this.carService.add(
                this.$scope.carCode.toUpperCase(),
                this.$scope.carType,
                this.$scope.carLength,
                this.$scope.driver,
                this.$scope.driverPhoneNumber,
                this.$scope.carrierCategory,
                Number(this.$scope.bearing),
                this.$scope.carSource,
                this.$scope.permitNumber,
                this.$scope.countrySubdivisionCode,
                this.$scope.roadTransportCertificateNumber,
                this.$scope.address,
                this.$scope.identity.toUpperCase()
            ).then(result => {
                //this.clearInput();
                let car = {
                    carId: result.data.extData,
                }
                window.localStorage.setItem("car", JSON.stringify(car));
                this.$ngBootbox.alert("保存成功！");
                this.$scope.saveDis = true;
                //this.$state.go("app.car.carManagent_edit", { carId: result.data.extData });
            }, (rej) => {
                this.$scope.saveDis = false;
            });
        });
    }
}

angular.module("tms").controller("carManagentAddController", ["$scope", "carService", "valueService", "$state", "$ngBootbox", CarManagentAddController]);